//
//  UIView+yu.m
//  PhotoSelector
//
//  Created by 李李善 on 2017/12/31.
//  Copyright © 2017年 洪雯. All rights reserved.
//

#import "UIView+yu.h"

typedef void (^NillBlock_Nill)(void);
#define CMM_AnimatePerior   0.35f
@implementation UIView (yu)
-(CGFloat)W{
    return self.frame.size.width;
}

-(CGFloat)H{
    return self.frame.size.height;
}

-(CGFloat)TX{
    return self.frame.origin.x;
}

-(CGFloat)TY{
    return self.frame.origin.y;
}

-(CGFloat)BX{
    return (self.frame.origin.x + self.frame.size.width);
}

-(CGFloat)BY{
    return (self.frame.origin.y + self.frame.size.height );
}



-(void)setFrame:(CGRect)frame animation:(BOOL)animated completion:(void(^)(BOOL))completion
{
    if (!animated) {
        [self setFrame:frame];
    } else {
        [UIView animateWithDuration:CMM_AnimatePerior animations:^{
            self.frame = frame;
        } completion:completion];
    }
}
-(void)move:(float)offset direct:(Direction)direction animation:(BOOL)animated
{
    CGRect rect = self.frame;
    switch (direction) {
        case Direct_Down:
            rect.origin.y += offset;
            break;
        case Direct_Up:
            rect.origin.y -= offset;
            break;
        case Direct_Left:
            rect.origin.x -= offset;
            break;
        case Direct_Right:
            rect.origin.x += offset;
            break;
    }
    
    [self setFrame:rect animation:animated completion:nil];
}

-(void)moveUp:(NSNumber*)offset animation:(BOOL)animated
{
    [self move:[offset floatValue] direct:Direct_Up animation:animated];
}

-(void)moveDown:(NSNumber*)offset animation:(BOOL)animated
{
    [self move:[offset floatValue] direct:Direct_Down animation:animated];
}


-(void)moveLeft:(NSNumber*)offset animation:(BOOL)animated
{
        [self move:[offset floatValue] direct:Direct_Left animation:animated];
}
-(void)moveRight:(NSNumber*)offset animation:(BOOL)animated
{
        [self move:[offset floatValue] direct:Direct_Right animation:animated];
}

static CGRect oldframe;
+(void)showImage:(UIButton*)headBtn{
    
    //UIImage *image = headBtn.currentBackgroundImage;
    UIImage *image = headBtn.imageView.image;
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    backgroundView.backgroundColor=[UIColor blackColor];
    backgroundView.alpha=0;
    
    
    oldframe=[headBtn convertRect:headBtn.bounds toView:window];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
    imageView.tag=1;
    imageView.image = image;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];
    
    
    
    
    NillBlock_Nill ShowImg = ^{
        imageView.frame=CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y, 0, 0);
        [UIView animateWithDuration:0.35 animations:^{
            imageView.frame=CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width);
            
            
            CGPoint center = imageView.center;
            center.y = [UIScreen mainScreen].bounds.size.height/2;
            imageView.center = center;
            
//            imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
            backgroundView.alpha = 1;
            imageView.alpha = 0.5;
        } completion:^(BOOL finished) {
            
        }];
    };
    
    
    ShowImg();

    
#define SP_width 60.f
    UIActivityIndicatorView* _spinnerView  = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _spinnerView.frame = CGRectMake((backgroundView.W - SP_width)/2, (backgroundView.H - SP_width)/2, SP_width, SP_width);
    [backgroundView addSubview:_spinnerView];
    [_spinnerView startAnimating];
    
    [_spinnerView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    
    
    //    NSString *headUrl = [NSString stringWithFormat:DownloadHeadUrl,[SDataCenter userLogonInfo].accessToken,headBtn.OBJ];
    //    [imageView sd_setImageWithURL:[NSURL URLWithString:headUrl] placeholderImage:headBtn.currentBackgroundImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    //        image = headBtn.currentBackgroundImage;
    //        [_spinnerView stopAnimating];
    //        imageView.alpha = 1 ;
    //    }];
}


+(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIButton *imageView=(UIButton*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}


@end
